package com.beeway.blackgoose.fragment;


import android.content.pm.ApplicationInfo;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.beeway.blackgoose.BaseApp;
import com.beeway.blackgoose.R;
import com.beeway.blackgoose.adapter.ListShowAdapter;
import com.beeway.blackgoose.bean.ListMessage;
import com.beeway.blackgoose.utils.Utils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class ListsFragment extends Fragment {
    private List<ListMessage> listDatas;
    private ListShowAdapter listAdapter;
    @ViewInject(R.id.lv_list_show)
    private SwipeMenuListView listLv;
    private View listView;

    public ListsFragment() {

    }
    public static ListsFragment getInstance() {
        ListsFragment list = new ListsFragment();
        return list;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        listView = inflater.inflate(R.layout.fragment_lists, container, false);
        ViewUtils.inject(this, listView);
        initView();
        return listView;
    }

    private void initView() {
        listDatas = new ArrayList<>();
        getData();
        listAdapter = new ListShowAdapter(BaseApp.getContext(),listDatas, Utils.getBitmapUtils(BaseApp.getContext()));
        listLv.setAdapter(listAdapter);
        SwipeMenuCreator creator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {

                // create "delete" item
                SwipeMenuItem deleteItem = new SwipeMenuItem(
                        BaseApp.getContext());
                // set item background
                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
                        0x3F, 0x25)));
                // set item width
                deleteItem.setWidth(dp2px(90));
                // set a icon
                deleteItem.setIcon(R.mipmap.delete);
                // add to menu
                menu.addMenuItem(deleteItem);
            }
        };
        // set creator
        listLv.setMenuCreator(creator);

        // step 2. listener item click event
        listLv.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                ListMessage item = listDatas.get(position);
                switch (index) {
                    case 0:
                        // delete
                        Toast.makeText(BaseApp.getContext(), "删除", Toast.LENGTH_SHORT).show();
//					delete(item);
                        listDatas.remove(position);
                        listAdapter.notifyDataSetChanged();
                        break;
                }
                return false;
            }
        });
    }

    public void getData() {
        for (int i = 0; i < 12; i++) {
            listDatas.add(new ListMessage("sd"+i,"12","sdfsdf"+i,null));
        }
    }

    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                getResources().getDisplayMetrics());
    }
}
